package com.atguigu.eduservice.controller.front;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.orderVo.CourseWebOrderVo;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.frontVo.CourseFrontVo;
import com.atguigu.eduservice.entity.vo.CourseWebVo;
import com.atguigu.eduservice.feign.OrderClient;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @ClassName EduCourseFrontController
 * @Description TODO
 * @Detail detail
 * @Author MyPC
 * @Date 2020/6/24
 * @Version 1.0
 */
@RestController
@RequestMapping("/eduservice/coursefront")
//@CrossOrigin
public class EduCourseFrontController {

    @Autowired
    EduCourseService eduCourseService;

    @Autowired
    EduChapterService chapterService;

    @Autowired
    OrderClient orderClient;

    @PostMapping("/getCourseFrontList/{page}/{size}")
    public R getCourseFrontList(@PathVariable("page") int page, @PathVariable("size") int size,
                               @RequestBody(required = false) CourseFrontVo vo){
        Page<EduCourse> coursePage=new Page<>(page,size);
        //需要返回多个参数可以使用map集合进行封装
        Map<String,Object> map=eduCourseService.getCourseFrontList(coursePage,vo);
        //使用map封装不需要指定key
        return R.ok().data(map);
    }

    //根据课程id查询课程详细信息
    @PostMapping("getCourseFrontInfo/{courseId}")
    public R getCourseFrontInfo(@PathVariable("courseId") String courseId, HttpServletRequest request){

        //根据课程id查询课程基础信息
        CourseWebVo courseWebVo=eduCourseService.getBaseCourseInfo(courseId);

        //根据课程id查询课程章节信息
        List<ChapterVo> chapterVideoList = chapterService.getChapterVideo(courseId);

        //根据课程id和会员id查询该用户是否购买过课程
        //TODO 如果用户未登录则无需远程调用查询用户是否购买
        boolean isBuy = orderClient.isBuyCourse(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return R.ok().data("courseWebVo",courseWebVo).data("chapterVideoList",chapterVideoList).data("isBuy",isBuy);
    }

    /**
     * 根据课程id获取课程信息
     * 生成订单时需要
     * @param courseId
     * @return
     */
    @PostMapping("getCourseFrontInfoOrder/{courseId}")
    public CourseWebOrderVo getCourseFrontInfoOrder(@PathVariable("courseId") String courseId){
        CourseWebVo courseWebVo = eduCourseService.getBaseCourseInfo(courseId);
        CourseWebOrderVo vo=new CourseWebOrderVo();
        BeanUtils.copyProperties(courseWebVo,vo);
        return vo;
    }

}
